package com.example.health_life.controller;

import com.example.health_life.Response;
import com.example.health_life.dao.Article;
import com.example.health_life.dao.New;
import com.example.health_life.service.NewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class NewController {

    @Autowired
    private NewService newService;

    @GetMapping("/news")
    public Response<List<New>> getNewsList(@RequestParam(required = false, defaultValue = "1") Integer status) {
        List<New> newsList = newService.getNewsListWithAuthor(status);
        return Response.newSuccess(newsList);
    }

    @GetMapping("/news/type")
    public Response<List<New>> getNewListByType(@RequestParam Byte type,
                                              @RequestParam(required = false, defaultValue = "1") Integer status) {
        List<New> newList = newService.getArticleListByTypeWithAuthor(type, status);
        return Response.newSuccess(newList);
    }

    @GetMapping("/news/{id}")
    public Response<New> getArticleById(@PathVariable Integer id) {
        New news = newService.getNewByIdWithAuthor(id);
        return Response.newSuccess(news);
    }

    @PostMapping("/news")
    public Response<Integer> addNew(@RequestBody New news) {
        int id = newService.addNew(news);
        return Response.newSuccess(id);
    }

    @DeleteMapping("/news/{id}")
    public Response<Integer> deleteNew(@PathVariable Integer id) {
        int result = newService.deleteNew(id);
        return Response.newSuccess(result);
    }

    @PutMapping("/news/{id}")
    public Response<Integer> updateNew(@PathVariable Integer id,
                                     @RequestParam(required = false, defaultValue = "null") String title,
                                     @RequestParam(required = false) String content,
                                     @RequestParam(required = false, defaultValue = "-9") Integer type,
                                     @RequestParam(required = false) String coverImg,
                                     @RequestParam(required = false, defaultValue = "-9") Integer view,
                                     @RequestParam(required = false, defaultValue = "-9") Integer favorite,
                                     @RequestParam(required = false, defaultValue = "-9") Integer status) {
        int result = newService.updateNew(id, title, content, type, coverImg, favorite, view, status);
        return Response.newSuccess(result);
    }
}
